package com.zhiyou.app.shop.manager.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.AycshopException;
import com.zhiyou.app.shop.common.ShopManageConstants;
import com.zhiyou.app.shop.common.util.FileUtil;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.PropertiesUtil;
import com.zhiyou.app.shop.common.util.ServiceUtil;
import com.zhiyou.app.shop.manager.dao.IFavShopDao;
import com.zhiyou.app.shop.manager.dao.IShopDao;
import com.zhiyou.app.shop.manager.dao.pojo.FavShop;
import com.zhiyou.app.shop.manager.dao.pojo.Goods;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.dao.pojo.UserDetail;
import com.zhiyou.app.shop.manager.service.IShopManageService;

import freemarker.template.utility.StringUtil;
@Service
public class ShopServiceImpl implements IShopManageService {

	@Autowired
	private IShopDao shopDao;
	
	@Autowired
	private IFavShopDao favShopDao;
	
	/****************************************商铺管理**************************************************/
	public Shop findMyShop(String userId) {
		List<Shop> list = shopDao.findByProperty("userDetail.id", userId);
		if(null == list || list.size()<1){
			throw new AycshopException("shopManage.remind.createShop");
		}
		return list.get(0);
	}

	public void saveOrUpdateMyShop(Shop shop) {
		
		DetachedCriteria dc = ServiceUtil.getADetachedCriteria(Shop.class);
		dc.add(Restrictions.eq("name", shop.getName()));
		if(StringUtils.isNotBlank(shop.getId())){
			dc.add(Restrictions.ne("id", shop.getId()));
		}
		List list = shopDao.findByCriteria(dc, null);
		if(null != list && list.size() > 0) throw new AycshopException("shopManage.shopName.replicate");
		
		if(StringUtils.isBlank(shop.getId())){
			//上传图片
			String imgPath = this.uploadImg(shop);
			shop.setImgPath(imgPath);
			
			shop.setCreateDate(new Date());
			shop.setUserDetail(ServiceUtil.getCurrentUser());
			shop.setOrderNumber(0);
			shopDao.save(shop);
		}else{
			Shop pojo = shopDao.findById(shop.getId());
			//如果图片被修改过
			String oldImgPath = null;
			if(null != shop.getShopImg()){
				String imgPath = this.uploadImg(shop);
				shop.setImgPath(imgPath);
				oldImgPath = pojo.getImgPath();
			}
			String[] ignoreProperties = {"id","orderNumber","userDetail","createDate","isDelete"};
			BeanUtils.copyProperties(shop, pojo,ignoreProperties);
			shopDao.update(pojo);
			
			if(null != oldImgPath) {//删除老图片
				String realpath = PropertiesUtil.getSysProperties("upload.img.basepath")+"/"+oldImgPath;
				File file = new File(realpath);
				FileUtil.deleteFiles(file);
			}
		}
	}

	private String uploadImg(Shop shop){
		String imgPath = FileUtil.aycShopUpload(shop.getShopImg(), shop.getShopImgFileName(), ShopManageConstants.SHOPIMGFORDER);
		return imgPath;
	}
	
	public void getShopBypage(Page page, Shop shop) {
		DetachedCriteria dc = ServiceUtil.getADetachedCriteria(Shop.class);
		if(null != shop.getIsDelete()){
			dc.add(Restrictions.eq("isDelete", shop.getIsDelete()));
		}
		if(StringUtils.isNotBlank(shop.getName())){
			dc.add(Restrictions.like("name", shop.getName().trim(), MatchMode.ANYWHERE));
		}
		shopDao.findByCriteria(page, dc, Order.desc("orderNumber"), Order.desc("createDate"));
	}

	public void closeOrOpenShop(String ids, boolean state) {
		shopDao.openOrCloseShop(ids, state);
	}

	public Shop findShopById(String id) {
		return shopDao.findById(id);
	}
	
	public void updateShop(Shop shopPojo) {
		shopDao.update(shopPojo);
	}

	public int getShopMaxOrder() {
		int number = shopDao.getMaxOrderNumber();
		return number+1;
	}
	
	
	public void addMyFavShop(UserDetail ud, Shop shop){
		DetachedCriteria dc = DetachedCriteria.forClass(FavShop.class);
		dc.add(Restrictions.eq("shop.id", shop.getId()));
		dc.add(Restrictions.eq("userDetail.id", ud.getId()));
		List list = favShopDao.findByCriteria(dc, null);
		if(null == list || list.size()<1){
			FavShop favShop = new FavShop();
			favShop.setShop(shop);
			favShop.setUserDetail(ud);
			favShop.setCreateDate(new Date());
			favShopDao.save(favShop);
		}
	}

	public void findMyFavShopByPage(Page page, UserDetail ud, Order order) {
		DetachedCriteria dc = DetachedCriteria.forClass(FavShop.class, "fav");
		dc.createAlias("fav.shop", "shop");
		dc.add(Restrictions.eq("fav.userDetail.id", ud.getId()));
		dc.add(Restrictions.eq("shop.isDelete", Boolean.FALSE));
		favShopDao.findByCriteria(page, dc, order);
		List<Object[]> list = page.getResult();
		List<Shop> listShop = new ArrayList<Shop>(list.size());
		page.setResult(listShop);
		if(null != list && list.size()>0){
			for(Object[] objs : list){
				listShop.add((Shop)objs[0]);
			}
		}
	}
	
	
}
